package com.rc.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rc.activity.R;
import com.rc.activity.ReceiptBoxDetailEditItem;
import com.rc.activity.dao.tbl_Category;
import com.rc.activity.dao.tbl_Item;
import com.rc.activity.dao.tbl_Merchant;
import com.rc.activity.dao.tbl_Receipt;
import com.rc.activity.dao.tbl_TripItems;
import com.rc.db.DatabaseSupport;
import com.rc.util.Util;

public class ReportDetailAdapter extends BaseExpandableListAdapter {

	private Activity activity;
	private LayoutInflater inflater;
	private ArrayList<tbl_Category> items;
	private ArrayList<HashMap<String, ArrayList<tbl_Item>>> arrHmItems = null;
	private OnItemChild child;
	private tbl_TripItems objTripItem;
	private ArrayList<tbl_Item> arrItem;
	private String reportType;

	public interface OnItemChild {
		public void setTotal();
	}

	public ReportDetailAdapter(ArrayList<tbl_Category> items,
			ArrayList<HashMap<String, ArrayList<tbl_Item>>> arrHmItems,
			OnItemChild child, String reportType) {
		this.items = items;
		this.arrHmItems = arrHmItems;
		this.child = child;
		this.reportType = reportType;
	}

	public void setInflater(LayoutInflater inflater, Activity activity) {
		this.inflater = inflater;
		this.activity = activity;
	}

	@Override
	public View getChildView(int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.report_detail_item, null);

			holder.txtItemAmount = (TextView) convertView
					.findViewById(R.id.txtItemAmount);
			holder.txtItemName = (TextView) convertView
					.findViewById(R.id.txtItemName);
			holder.txtPurchaseTime = (TextView) convertView
					.findViewById(R.id.txtPurchaseTime);
			holder.txtMerchantName = (TextView) convertView
					.findViewById(R.id.txtMerchantName);
			holder.llUnClaim = (LinearLayout) convertView
					.findViewById(R.id.llUnClaim);
			holder.llClaim = (LinearLayout) convertView
					.findViewById(R.id.llClaim);
			holder.txtUnclaim = (TextView) convertView
					.findViewById(R.id.txtUnclaim);
			holder.txtClaim = (TextView) convertView
					.findViewById(R.id.txtClaim);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		arrItem = new ArrayList<tbl_Item>();
		arrItem = arrHmItems.get(groupPosition).get(
				items.get(groupPosition).getName());
		tbl_Item objItem = new tbl_Item();
		objItem = arrItem.get(childPosition);
		holder.txtItemAmount.setText("$" + objItem.getAmount() + "");
		holder.txtItemName.setText(objItem.getName());

		// get Object Receipt
		tbl_Receipt objReceipt = new tbl_Receipt();
		objReceipt = DatabaseSupport.getReceiptByID(objItem.getReceiptID(),
				activity);
		if (objReceipt != null) {
			holder.txtPurchaseTime.setText(objReceipt.getPurchaseTime());

			// get Object Merchant
			tbl_Merchant objMerchant = new tbl_Merchant();
			objMerchant = DatabaseSupport.getMerchantByID(
					objReceipt.getMerchantID(), activity);
			if (objMerchant != null) {
				holder.txtMerchantName.setText(objMerchant.getName());
			}

		}
		// get Trip Items
		objTripItem = new tbl_TripItems();
		objTripItem = DatabaseSupport.getTripItemsByID(objItem.getItemID(),
				activity);
		if (objTripItem != null) {

			if (reportType.equals(activity.getString(R.string.draft_reports))) {
				if (objTripItem.getIsClaimed() == 1) {
					holder.llClaim.setVisibility(View.GONE);
					holder.llUnClaim.setVisibility(View.VISIBLE);
				} else {
					holder.llClaim.setVisibility(View.VISIBLE);
					holder.llUnClaim.setVisibility(View.GONE);
				}
				holder.llClaim.setVisibility(View.VISIBLE);
				holder.llUnClaim.setVisibility(View.VISIBLE);
			} else if (reportType.equals(activity
					.getString(R.string.pending_reports))) {
				if (objTripItem.getIsApproved() == 1) {
					holder.llClaim.setVisibility(View.GONE);
					holder.llUnClaim.setVisibility(View.VISIBLE);
				} else {
					holder.llClaim.setVisibility(View.VISIBLE);
					holder.llUnClaim.setVisibility(View.GONE);
				}
				holder.llUnClaim
						.setBackgroundResource(R.drawable.background_claim);
				holder.llClaim
						.setBackgroundResource(R.drawable.background_approver);
				holder.txtClaim.setText(activity.getString(R.string.approver));
				holder.txtUnclaim.setText(activity.getString(R.string.reject));
			} else {
				holder.llClaim.setVisibility(View.GONE);
				holder.llUnClaim.setVisibility(View.GONE);
			}
			holder.llClaim.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					tbl_Item objItem = new tbl_Item();
					objItem = arrItem.get(childPosition);
					if (reportType.equals(activity
							.getString(R.string.draft_reports))) {
						DatabaseSupport
								.updateDataClaim(objItem, true, activity);
						holder.llClaim.setVisibility(View.GONE);
						holder.llUnClaim.setVisibility(View.VISIBLE);
					} else if (reportType.equals(activity
							.getString(R.string.pending_reports))) {
						DatabaseSupport.updateDataReject(objItem, true,
								activity);
						holder.llUnClaim
								.setBackgroundResource(R.drawable.background_claim);
						holder.txtClaim.setText(activity
								.getString(R.string.approver));
						holder.llClaim.setVisibility(View.GONE);
						holder.llUnClaim.setVisibility(View.VISIBLE);
					}

					child.setTotal();
				}
			});
			holder.llUnClaim.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// get Object Receipt

					tbl_Item objItem = new tbl_Item();
					objItem = arrItem.get(childPosition);
					if (reportType.equals(activity
							.getString(R.string.draft_reports))) {
						DatabaseSupport.updateDataClaim(objItem, false,
								activity);
						holder.llClaim.setVisibility(View.VISIBLE);
						holder.llUnClaim.setVisibility(View.GONE);
					} else if (reportType.equals(activity
							.getString(R.string.pending_reports))) {
						DatabaseSupport.updateDataReject(objItem, false,
								activity);
						holder.llClaim
								.setBackgroundResource(R.drawable.background_approver);
						holder.txtClaim.setText(activity
								.getString(R.string.approver));
						holder.llClaim.setVisibility(View.GONE);
						holder.llUnClaim.setVisibility(View.VISIBLE);
					}
					child.setTotal();
				}
			});
			convertView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					tbl_Item objItem = new tbl_Item();
					objItem = arrItem.get(childPosition);
					ReceiptBoxDetailEditItem.callIntentReportDetail(activity,
							objItem, reportType);
				}
			});
		}

		return convertView;
	}

	static class ViewHolder {
		TextView txtCategories, txtTotal, txtItemAmount, txtItemName,
				txtPurchaseTime, txtMerchantName, txtClaim, txtUnclaim;
		LinearLayout llClaim, llUnClaim;
		ImageView imgView_down, imgView_up;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(
					R.layout.report_detail_categories_item, null);
			holder.txtCategories = (TextView) convertView
					.findViewById(R.id.txtCategories);
			holder.txtTotal = (TextView) convertView
					.findViewById(R.id.txtTotal);
			holder.imgView_down = (ImageView) convertView
					.findViewById(R.id.imgView_down);
			holder.imgView_up = (ImageView) convertView
					.findViewById(R.id.imgView_up);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		ArrayList<tbl_Item> arrItem = new ArrayList<tbl_Item>();
		arrItem = arrHmItems.get(groupPosition).get(
				items.get(groupPosition).getName());
		float total = 0;
		if (arrItem != null) {
			for (tbl_Item objItem : arrItem) {
				total = total + Util.convertFloat(objItem.getAmount());
			}
		}
		if (holder.txtTotal != null) {
			holder.txtTotal.setText("$" + Util.formatStringFloat(total) + "");
		}
		if (holder.txtCategories != null) {
			holder.txtCategories.setText(items.get(groupPosition).getName());
		}

		return convertView;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return arrHmItems.get(groupPosition)
				.get(items.get(groupPosition).getName()).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return null;
	}

	@Override
	public int getGroupCount() {
		return items.size();
	}

	@Override
	public void onGroupCollapsed(int groupPosition) {
		super.onGroupCollapsed(groupPosition);
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		super.onGroupExpanded(groupPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
